from main import Check_sys
import sys
import os
from os.path import exists

def add_age():
    list = []
    with open(os.getenv('HOME')+"/dd.csv") as file:
        file.readline()
        for line in file.read().splitlines():
            fields = line.split('\t')
            print(fields[0], fields[3])
            if int(fields[0]) < 112:
                continue
            for rec in Check_sys.select().where(Check_sys.number=='%05d'%int(fields[0])):
                rec.age = int(fields[3])
                # rec.name = fields[1]
                # rec.created_date =fields[4].replace('-','_')
                # rec.eye_sight = fields[7] if rec.which_eye=='R' else fields[8]
                # rec.eye_sight = fields[7] if rec.which_eye=='R' else fields[8]
                rec.save()


def change_image_ext():
    ll = Check_sys.select()
    for index,rec in enumerate(ll):
        print(index,'/',len(ll),end='\r')
        fname = rec.original_image
        if not fname.endswith('.png'):
            continue
        fname = fname[:-4]+'.jpg'
        if not exists(fname):
            print("Cannot find ", fname)
            continue
        rec.original_image = fname
        rec.ha_img = rec.ha_img[:-4]+'.jpg'
        rec.ex_img = rec.ex_img[:-4]+'.jpg'
        rec.save()

if __name__ == '__main__':
    change_image_ext()